#!/bin/bash
#
#dev:enCEE
#vars
#
#functions
arch_wireless()
{
		clear
		echo "-------------------------------------------"
		echo "-                                         -"
		echo "-       Arch Network Configuration        -"
		echo "-                                         -"
		echo "-           WIFI CONNECTION               -"
		echo "-                                         -"
		echo "-         For help type 'Help'            -"
		echo "-                                         -"
		echo "-------------------------------------------"
		echo "-                                         -"
		echo "-                                         -"
		echo "-1) Manual Setup                          -"
		echo "-2) Automatic Setup                       -"
		echo "-                                         -"
		echo "-[H]elp -Open help document               -"                    
		echo "-[B]ack -Return to Network Configuration  -"
		echo "-[Q]uit -Quit and return to main menu     -"
		echo "-------------------------------------------"
		echo "What is your selection?"
		read wifi_setup
		for(( x = 0; x < 1;))
			do
			case $wifi_setup in
				1)
					arch_wifi_man
					x = 1
				;;
				2)
					arch_wifi_auto
					x = 1
				;;
				"B" | "b" | "Back" | "back")
					arch_netconf_submenu
				;;
				"Q" | "q" | "Quit" | "quit" )
					sys_scripts_dir/arch_menu
				;;
				"Help" | "help" | "H" | "h" )
					sys_scripts_dir/arch_help
				;;
				*)
					echo "Invalid Response"
					echo "Will this be manual or automatic?"
					echo "Enter 1 for manual, 2 for automatic, or Q to quit"
					read wifi_setup
					arch_wireless
				;;				
			esac
		done
}
arch_wifi_man()
{
	echo "Under Maintenance"
	echo "Press [enter] to return to the wireless menu"
	read ""
	sys_scripts_dir/arch_wireless
}
arch_wifi_auto()
{
		echo "You selected automatic wireless connection"
		echo "You will be prompted to select your wireless network, and enter password."
		echo "Press [enter] to continue..."
		read prompt
		echo "Starting your wireless..."
		if wifi-menu wlan0 = 1
		then
			return 1
			echo "No wireless card detected"
			echo "Press [enter] to continue..."
		else wifi-menu wlan0 = 0
			echo "Wireless connection successful"
			echo "Testing connection..."
			arch_ping_test
		fi
		read complete
		if $complete
		then 
			sys_scripts_dir/arch_menu
		else
			sys_scripts_dir/arch_wireless
		fi
}
arch_wired()
{
		clear
		echo "-------------------------------------------"
		echo "-                                         -"
		echo "-       Arch Network Configuration        -"
		echo "-                                         -"
		echo "-           WIRED CONNECTION              -"
		echo "-                                         -"
		echo "-         For help type 'Help'            -"
		echo "-                                         -"
		echo "-------------------------------------------"
		echo "-                                         -"
		echo "-                                         -"
		echo "-1) Static Setup                          -"
		echo "-2) DHCP Setup                            -"
		echo "-                                         -"
		echo "-[H]elp -Open help document               -"                    
		echo "-[B]ack -Return to Network Configuration  -"
		echo "-[Q]uit -Quit and return to main menu     -"
		echo "-------------------------------------------"
		echo "What is your selection?"
		read wired_setup
		for(( x = 0; x < 1;))
			do
			case $wired_setup in
				1)
					arch_static
					x = 1
				;;
				2)
					arch_dhcp
					x = 1
				;;
				"B" | "b" | "Back" | "back")
					arch_netconf_submenu
				;;
				"Q" | "q" | "Quit" | "quit" )
					sys_scripts_dir/arch_menu
				;;
				"Help" | "help" | "H" | "h" )
					sys_scripts_dir/arch_help
				;;
				*)
					echo "Invalid Response"
					echo "Will this be manual or automatic?"
					echo "Enter 1 for manual, or 2 for automatic"
					read wired_setup
				;;
			esac
		done
}
arch_static()
{
		echo "You have selected the manual setup process"
		echo "Please enter your static IP address."
			read static_ip
		echo "Please enter your Subnet Mask."
			read static_smask
		echo "Please enter the DNS IP address."
			read static_dns
		echo "Please enter the Domain Name"
			read static_dname
		echo "Manual setup complete"
		arch_static_review
		echo "Pinging google.com ... "
		arch_ping_test
		read complete
		if $complete
		then 
			sys_scripts_dir/arch_menu
		else
			sys_scripts_dir/arch_wired
		fi
}
arch_dhcp()
{
		echo "Setting up the connection..."
		ip link set eth0 up
		dhcpd eth0
		arch_ping_test
		read complete
		if $complete
		then 
			sys_scripts_dir/arch_menu
		else
			sys_scripts_dir/arch_wired
		fi
		
}
arch_ping_test()
{
	count = 100
		while [[ $count -ne 0 ]] ;
			do
			ping google.com -c 1 >> /dev/null
			rc=$?
			if [[ $rc -eq 0 ]]; 
			then
				count = 1
			fi
			((count = count - 1))
		done
		
		if [[ $rc -eq 0 ]] ;
		then
			echo "Internet connection was successful!"
			echo ""
			echo -n "Press [enter] to continue"
			return 0
		else
			echo "Internet connection could not be completed."
			echo "Google is up 99.99% of the time!"
			echo ""
			echo -n "Press [enter] to continue"
			return 1
		fi
}
arch_static_review ()
{
	echo "Would you like to review your entries? [y/n]"
		read review
		case $review in
			"Y" | "y" )
						echo ""
						echo ""
						echo "Static IP Address : $static_ip"
						echo "Subnet Mask : $static_smask"
						echo "DNS IP Address : $static_dns"
						echo "Domain Name : $static_dname"
						echo ""
						echo "To change any selections..."
						echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or Q to continue"
						read edit
						for(( x = 0; x < 1;))
						do
						case $edit in
							1)
								echo "Please enter your Static IP address."
								read static_ip
								echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or R to review your entries and continue"
								read edit
							;;
							2)
								echo "Please enter your Subnet Mask."
								read static_smask
								echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or R to review your entries and continue"
								read edit
							;;
							3)
								echo "Please enter your DNS IP address."
								read static_dns
								echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or R to review your entries and continue"
								read edit
							;;
							4) 
								echo "Please enter the Domain Name"
								read static_dname
								echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or R to review your entries and continue"
								read edit
							;;
							"R" | "r" )
								echo ""
								echo "Static IP Address : $static_ip"
								echo "Subnet Mask : $static_smask"
								echo "DNS IP Address : $static_dns"
								echo "Domain Name : $static_dname"
								echo ""
								x = 1
							;;
							*)
								echo "Invalid selection"
								echo "Press 1 to change the IP, 2 to change the Subnet Mask, 3 to change the DNS IP, 4 to change the Domain Name, or R to review your entries and continue"
								read edit
							;;
						esac
						done
					;;
			"N" | "n" )
						echo "No review required..."
						echo " "
						echo " "
					;;
		esac
}
arch_netconf_submenu()
{
clear
echo "-------------------------------------------"
echo "-                                         -"
echo "-     Arch Network Configuration          -"
echo "-                                         -"
echo "-        For help type 'Help'             -"
echo "-                                         -"
echo "-------------------------------------------"
echo "-                                         -"
echo "-                                         -"
echo "-1) Wired Setup                           -"
echo "-2) Wireless Setup                        -"
echo "-                                         -"
echo "-[H]elp -Open help document               -"                     
echo "-[Q]uit -Quit and return to main menu     -"
echo "-------------------------------------------"
echo "What is your selection?"
read selection
for(( x = 0; x < 1;))
		do 	
		case $selection in
			1)  
				arch_wired
				x = 1
			;;
			2) 
				arch_wireless
				x = 1
			;;
			"Q" | "q" | "Quit" | "quit" )
				sys_scripts_dir/arch_menu
			;;
			"H" | "h" |"Help" | "help")
					sys_scripts_dir/arch_help
			;;
			*) 
				echo "Invalid Response"
				echo "Will this be wired or wireless?"
	            echo "Enter 1 for wired, 2 for wireless, or Q to quit and return to the main menu"
	            read network
			;;
		esac
	done
}
#
#script
arch_netconf_submenu
#end
